package com.jsmy.softapp.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jsmy.softapp.R;
import com.jsmy.softapp.adapter.DataFragmentAdapter;
import com.jsmy.softapp.aroute.AR;
import com.jsmy.softapp.bean.TXLBean;
import com.jsmy.softapp.net.API;
import com.jsmy.softapp.presenter.DataFPresenter;
import com.jsmy.softapp.view.DataFView;

import java.util.List;

import butterknife.BindView;

/**
 * Created with Android Studio.
 * User: wei_cd.
 * Date: 2018/4/17.
 * Time: 17:48.
 * To change for file | settings | file and code templates.
 */
@Route(path = AR.F_DATA)
public class DataFragment extends BaseFragment<DataFView, DataFPresenter> implements DataFView {

    @BindView(R.id.m_expandable)
    ExpandableListView mExpandable;
    DataFragmentAdapter adapter;
    List<TXLBean.DataBean.ListBeanX> list;

    @Override
    protected DataFPresenter createPresenter() {
        return new DataFPresenter();
    }

    @Override
    protected int setView() {
        return R.layout.fragment_data;
    }

    @Override
    protected void init(View view) {

        mExpandable.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                adapter.setIndicatorState(groupPosition, true);
            }
        });
        mExpandable.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
                adapter.setIndicatorState(groupPosition, false);
            }
        });
        mExpandable.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                ARouter.getInstance().build(AR.A_CUSTOMER)
                        .withString("kid",list.get(groupPosition).getList().get(childPosition).getKid())
                        .navigation(mActivity,101);
                return true;
            }
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        presenter.gettxllist();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showLoadingData(String type, Object object) {
        switch (type){
            case API.APP_TXL_LIST:
                list = ((TXLBean)object).getData().getList();
                adapter = new DataFragmentAdapter(getActivity(), list);
                mExpandable.setAdapter(adapter);
                break;
        }
    }

    @Override
    public void onDestroyView() {
        presenter.model.disposable.clear();
        super.onDestroyView();
    }

    @Override
    public void updata() {
        presenter.gettxllist();
    }
}
